package cn.iocoder.yudao.module.promotion.service.prizedraw;

import cn.hutool.core.collection.CollUtil;
import cn.iocoder.yudao.framework.common.pojo.PageResult;
import cn.iocoder.yudao.framework.common.util.object.BeanUtils;
import cn.iocoder.yudao.module.promotion.controller.admin.prizedraw.vo.PrizeDrawLotteryPageReqVO;
import cn.iocoder.yudao.module.promotion.controller.admin.prizedraw.vo.PrizeDrawLotterySaveReqVO;
import cn.iocoder.yudao.module.promotion.dal.dataobject.prizedraw.PrizeDrawLotteryDO;
import cn.iocoder.yudao.module.promotion.dal.mysql.prizedraw.PrizeDrawLotteryMapper;
import org.springframework.stereotype.Service;
import org.springframework.validation.annotation.Validated;

import javax.annotation.Resource;
import java.util.List;

import static cn.iocoder.yudao.framework.common.exception.util.ServiceExceptionUtil.exception;
import static cn.iocoder.yudao.module.promotion.enums.ErrorCodeConstants.PRIZE_DRAW_LOTTERY_NOT_EXISTS;

/**
 * 抽奖活动中奖 Service 实现类
 *
 * @author 芋道源码
 */
@Service
@Validated
public class PrizeDrawLotteryServiceImpl implements PrizeDrawLotteryService {

    @Resource
    private PrizeDrawLotteryMapper prizeDrawLotteryMapper;

    @Override
    public Long createPrizeDrawLottery(PrizeDrawLotterySaveReqVO createReqVO) {
        // 插入
        PrizeDrawLotteryDO prizeDrawLottery = BeanUtils.toBean(createReqVO, PrizeDrawLotteryDO.class);
        prizeDrawLotteryMapper.insert(prizeDrawLottery);
        // 返回
        return prizeDrawLottery.getPrizeDrawLotteryId();
    }

    @Override
    public void updatePrizeDrawLottery(PrizeDrawLotterySaveReqVO updateReqVO) {
        // 校验存在
        validatePrizeDrawLotteryExists(updateReqVO.getPrizeDrawLotteryId());
        // 更新
        PrizeDrawLotteryDO updateObj = BeanUtils.toBean(updateReqVO, PrizeDrawLotteryDO.class);
        prizeDrawLotteryMapper.updateById(updateObj);
    }

    @Override
    public void deletePrizeDrawLottery(Long id) {
        // 校验存在
        validatePrizeDrawLotteryExists(id);
        // 删除
        prizeDrawLotteryMapper.deleteById(id);
    }

    @Override
        public void deletePrizeDrawLotteryListByIds(List<Long> ids) {
        // 校验存在
        validatePrizeDrawLotteryExists(ids);
        // 删除
        prizeDrawLotteryMapper.deleteByIds(ids);
        }

    private void validatePrizeDrawLotteryExists(List<Long> ids) {
        List<PrizeDrawLotteryDO> list = prizeDrawLotteryMapper.selectByIds(ids);
        if (CollUtil.isEmpty(list) || list.size() != ids.size()) {
            throw exception(PRIZE_DRAW_LOTTERY_NOT_EXISTS);
        }
    }

    private void validatePrizeDrawLotteryExists(Long id) {
        if (prizeDrawLotteryMapper.selectById(id) == null) {
            throw exception(PRIZE_DRAW_LOTTERY_NOT_EXISTS);
        }
    }

    @Override
    public PrizeDrawLotteryDO getPrizeDrawLottery(Long id) {
        return prizeDrawLotteryMapper.selectById(id);
    }

    @Override
    public PageResult<PrizeDrawLotteryDO> getPrizeDrawLotteryPage(PrizeDrawLotteryPageReqVO pageReqVO) {
        return prizeDrawLotteryMapper.selectPage(pageReqVO);
    }

}